local addon = CreateFrame("Frame")
local playerGuid = nil
local format = "%s: %s (%s)"
local grouped = nil
local lastTimestamp = nil
local lastInterrupted = nil
local chanType, channel = "PARTY", nil
local channelMap = {
	s = "SAY", say = "SAY",
	y = "YELL", yell = "YELL",
	p = "PARTY", party = "PARTY",
	ra = "RAID", raid = "RAID",
	rw = "RAID_WARNING",
	bg = "BATTLEGROUND",
}
local chatChannels = {}
local function populateChannels(...)
	wipe(chatChannels)
	for i = 1, select("#", ...), 2 do
		local id, name = select(i, ...)
		chatChannels[id] = name
	end
end

addon:SetScript("OnEvent", function(self, e, ts, cleu, _, src, _,_,_,_, dst, _,_,_, spell, _, iId, iSpell)
	if e == "PARTY_MEMBERS_CHANGED" then
		local n = GetNumPartyMembers()
		if not grouped and n > 0 then
			self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			grouped = true
		elseif grouped and n == 0 then
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			grouped = nil
		end
	elseif e == "CHANNEL_UI_UPDATE" then
		populateChannels(GetChannelList())
	elseif e == "PLAYER_LOGIN" then
		playerGuid = UnitGUID("player")
	elseif e == "ADDON_LOADED" and ts == "GnomishInterrupter" then
		if type(GnomishInterrupterChannel) == "nil" then
			GnomishInterrupterChannel = "PARTY"
		end
		populateChannels(GetChannelList())
		self:UnregisterEvent("ADDON_LOADED")
	elseif e == "COMBAT_LOG_EVENT_UNFILTERED" then
		if cleu == "SPELL_INTERRUPT" and src == playerGuid then
			if ts ~= lastTimestamp or iSpell ~= lastInterrupted then
				lastTimestamp = ts
				lastInterrupted = iSpell
				if type(GnomishInterrupterChannel) == "number" then
					local msg = format:format(spell, iSpell, dst)
					SendChatMessage(msg, "CHANNEL", nil, GnomishInterrupterChannel)
				else
					local msg = format:format(spell, (GetSpellLink(iId)) or iSpell, dst)
					SendChatMessage(msg, GnomishInterrupterChannel)
				end
			end
		end
	end
end)
addon:RegisterEvent("PARTY_MEMBERS_CHANGED")
addon:RegisterEvent("PLAYER_LOGIN")
addon:RegisterEvent("ADDON_LOADED")
addon:RegisterEvent("CHANNEL_UI_UPDATE")

SlashCmdList.GnomishInterrupter = function(input)
	if input == "off" then
		addon:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		print("GnomishInterrupter disabled until you join a new group, which automatically enables it again.")
	elseif input == "on" then
		addon:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
		print("GnomishInterrupter forcefully enabled.")
	else
		if type(tonumber(input)) == "number" then input = tonumber(input) end
		local c = nil
		for k, v in pairs(channelMap) do
			if input == k then c = v; break
			elseif input == v then c = v; break
			end
		end
		for k, v in pairs(chatChannels) do
			if input == k then c = k; break
			elseif input == v then c = k; break
			end
		end
		if c then
			print(("Now printing to channel %s."):format(c))
			GnomishInterrupterChannel = c
		else
			print("Usage: /ginterrupt <[channel]|on|off>")
			print("[channel] can be say, party, raid, raid_warning, battleground or a channel index.")
		end
	end
end
SLASH_GnomishInterrupter1 = "/ginterrupt"

